package com.evan.uyclub;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import com.evan.uyclub.Other.LoadDialog;
import com.evan.uyclub.Other.SetClass;
import com.evan.uyclub.Other.Utlis;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sdsmdg.tastytoast.TastyToast;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;

public class ActivityReg extends AppCompatActivity {
    private BottomSheetDialog dialog;
    private LoadDialog loadDialog;
    private String user;
    private String pass = "jfkjcyxf666";
    private String name;
    private TwinklingRefreshLayout smart;
    private LinearLayout all_main_reg;
    private MaterialEditText users, names;
    private CardView reg;
    private TextView zc_text, zc_text1;
    private SharedPreferences data;

    public void ActivityRegd() {
        loadDialog = new LoadDialog(this);
        loadDialog.show(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        //获取文本设置动画和字体
        zc_text = findViewById(R.id.zc_text);
        zc_text1 = findViewById(R.id.zc_text1);
        zc_text.setTypeface(Typeface.createFromAsset(getAssets(), "english.ttf"));
        zc_text1.setTypeface(Typeface.createFromAsset(getAssets(), "rubik.ttf"));
        //实现回弹效果
        smart = findViewById(R.id.smart);
        smart.setPureScrollModeOn();
        all_main_reg = findViewById(R.id.all_main_regs);
        //和逐渐显示动画
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(all_main_reg, "alpha", 0f, 1f);
        objectAnimator.setDuration(1500);
        objectAnimator.start();
        users = findViewById(R.id.user);
        names = findViewById(R.id.name);
        reg = findViewById(R.id.reg);
        Reg();
    }

    private void Reg() {
        reg.setOnClickListener(view -> {
            user = Objects.requireNonNull(users.getText()).toString();
            name = Objects.requireNonNull(names.getText()).toString();
            if (user.equals("") || name.equals("")) {
                TastyToast.makeText(ActivityReg.this, "输入信息哦~", TastyToast.LENGTH_SHORT, TastyToast.CONFUSING);
            } else {
                ActivityRegd();
                new Handler().postDelayed(this::Regs, 1500);
            }
        });
    }

    private void Regs() {
        TastyToast.makeText(ActivityReg.this, "已关闭注册登录功能", TastyToast.LENGTH_SHORT, TastyToast.INFO);
        if (!Utlis.isVPNConnected(this)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(SetClass.getUrl() + "uyapi/uyyun_userreg.php?" + "keeper=" + SetClass.getAdmin() + "&api=" + SetClass.getApi() + "&user=" + ActivityReg.this.user + "&pass=" + pass + "&name=" + name);
                        URLConnection backtraclk = url.openConnection();
                        InputStreamReader back = new InputStreamReader(backtraclk.getInputStream());
                        BufferedReader Readerl = new BufferedReader(back);
                        String datas = Readerl.readLine();
                        if (datas.contains("注册成功")) {
                            data = getSharedPreferences("data", MODE_PRIVATE);
                            SharedPreferences.Editor editor = data.edit();
                            editor.putString("user", user);
                            editor.commit();
                            loadDialog.dismiss();
                            Looper.prepare();
                            TastyToast.makeText(ActivityReg.this, "注册成功喽~", TastyToast.LENGTH_SHORT, TastyToast.SUCCESS);
                            Intent intent = new Intent();
                            intent.setClass(ActivityReg.this, StartActivity.class);
                            startActivity(intent);
                            overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);
                            Looper.loop();
                            finish();
                        } else {
                            loadDialog.dismiss();
                            Looper.prepare();
                            TastyToast.makeText(ActivityReg.this, datas, TastyToast.LENGTH_SHORT, TastyToast.ERROR);
                            Looper.loop();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public void reg_end(View view) {
        //退出界面，直接退app
       /*finish();
       overridePendingTransition(R.anim.activity_visible,R.anim.activity_gone);**/
        TastyToast.makeText(ActivityReg.this, "亲，好的在里面呢~", TastyToast.LENGTH_SHORT, TastyToast.INFO);
    }

}